﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI;
using EmptyWallGallery.Core;

namespace EmptyWallGallery.FrontEnd.inside.Controls
{
    public partial class ProductCategory : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadCategory();
        }

        public void LoadCategory()
        {
            CatDetail.InnerHtml = Core.BLL.Categories.Instance.WriteCategoryInside(1, Configs.SiteId);
            cboCategory.InnerHtml = Core.BLL.Categories.Instance.WriteCategoryInside(2, Configs.SiteId);
        }

        protected void BtnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtTitle.Text == "")
                {
                    lblError.Attributes.Remove("style");
                    lblError.InnerHtml = "* Xin vui lòng nhập tên danh mục !!";
                    txtTitle.Focus();
                }
                else
                {
                    if (txtTitle.Text.Length > 50)
                    {
                        lblError.Attributes.Remove("style");
                        lblError.InnerHtml = "* Tên danh mục quá dài !!";
                        txtTitle.Focus();
                    }
                    else
                    {
                        if (txtDesc.Text.Length > 450)
                        {
                            lblError.Attributes.Remove("style");
                            lblError.InnerHtml = "* Mô tả quá dài !!";
                            txtDesc.Focus();
                        }
                        else
                        {
                            if (!Core.Utilities.Common.Instance.IsNumeric(txtCatIndex.Text))
                            {
                                lblError.Attributes.Remove("style");
                                lblError.InnerHtml = "* Vị trí phải là số tự nhiên!!";
                                txtDesc.Focus();
                            }
                            else
                            {
                                var subCat = 0;
                                var subCatx = 0;

                                if (Request.Form["cboCategory"] != "None")
                                {
                                    var str = Regex.Split(Request.Form["cboCategory"], ",");
                                    subCat = int.Parse(str[0]);
                                    subCatx = int.Parse(str[1]);

                                    if (subCatx != 0)
                                    {
                                        subCatx = int.Parse(str[0]);
                                        subCat = int.Parse(str[1]);
                                    }
                                }

                                Core.BLL.Categories.Instance.Create(txtTitle.Text.Trim(), txtDesc.Text.Trim(),
                                                                    Core.Utilities.Common.Instance.Utf8ToAscii(
                                                                        txtTitle.Text.Trim()),
                                                                    int.Parse(txtCatIndex.Text),
                                                                    subCat, subCatx, Configs.SiteId);

                                txtTitle.Enabled = false;
                                txtCatIndex.Enabled = false;
                                txtDesc.Enabled = false;
                                BtnCreate.Enabled = false;
                                btnApply.Enabled = false;
                                btnApply2.Enabled = false;

                                LoadCategory();
                                Core.BLL.Categories.Instance.WriteCategory(true);
                                
                                txtTitle.Enabled = true;
                                txtCatIndex.Enabled = true;
                                txtDesc.Enabled = true;
                                txtCatIndex.Text = "0";
                                txtDesc.Text = "";
                                txtTitle.Text = "";
                                BtnCreate.Enabled = true;
                                btnApply.Enabled = true;
                                btnApply2.Enabled = true;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void BtnApplyClick(object sender, EventArgs e)
        {
            try
            {
                const int i = 0;
                if (Request.Form["selectedAction0"] == "1" || Request.Form["selectedAction1"] == "1")
                {
                    //Edit Category
                    var vId = "";
                    foreach (var str in Regex.Split(Request.Form["cboCheck"], ",").Where(str => i == 0))
                    {
                        vId = str;
                        if (!Core.Utilities.Common.Instance.IsNumeric(vId))
                        {
                            vId = "1";
                        }
                        break; // TODO: might not be correct. Was : Exit For
                    }
                    Response.Redirect("default.aspx?action=Edit&CatID=" + vId, false);
                }
                else if (Request.Form["selectedAction0"] == "2" || Request.Form["selectedAction1"] == "2")
                {
                    //Delete
                    foreach (var str in Regex.Split(Request.Form["cboCheck"], ","))
                    {
                        DeleteCat(int.Parse(str));
                    }
                    Core.BLL.Categories.Instance.WriteCategory(true);
                    Response.Redirect("default.aspx", false);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void BtnApply2Click(object sender, EventArgs e)
        {
            BtnApplyClick(sender, e);
        }

        public void DeleteCat(int vId)
        {
            Core.BLL.Categories.Instance.Delete(vId, Configs.SiteId);
        }

    }
}